package com.example.smartcity1.my;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.MainActivity;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.MyLoginBean;
import com.example.smartcity1.bean.MyLoginPostBean;
import com.example.smartcity1.bean.MyUserInfoBean;
import com.example.smartcity1.network.RetrofitClient;
import com.example.smartcity1.network.Utils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyLoginActivity extends AppCompatActivity {
    EditText editUsername;
    EditText editPassword;
    SharedPreferences user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_login);

        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("登录");
        supportActionBar.setDisplayHomeAsUpEnabled(true);

        editUsername = findViewById(R.id.my_login_username);
        editPassword = findViewById(R.id.my_login_password);
        Button buttonLogin = findViewById(R.id.my_login_login);
        Button register = findViewById(R.id.my_login_register);

        user = getSharedPreferences("user",MODE_PRIVATE);

        buttonLogin.setOnClickListener(v -> {
            String username = editUsername.getText().toString();
            String password = editPassword.getText().toString();
            RetrofitClient.appService().getMyLoginData(new MyLoginPostBean(username,password)).enqueue(new Callback<MyLoginBean>() {
                @Override
                public void onResponse(Call<MyLoginBean> call, Response<MyLoginBean> response) {
                    MyLoginBean loginBean = response.body();
                    if (loginBean != null) {
                        if (loginBean.getCode() == 200) {
                            SharedPreferences.Editor editor = getSharedPreferences("user",MODE_PRIVATE).edit();
                            editor.putString("username", username);
                            editor.putString("password", password);
                            editor.putString("token",loginBean.getToken());
                            editor.apply();
                            Utils.login = 1;
                            getUserId();
                            finish();
                        } else {
                            Toast.makeText(MyLoginActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
                            editPassword.setText("");
                        }
                    }
                }
                @Override
                public void onFailure(Call<MyLoginBean> call, Throwable t) {

                }
            });
        });
        register.setOnClickListener(v -> startActivity(new Intent(MyLoginActivity.this, MyRegisterActivity.class)));//注册
    }

    private void getUserId() {
        RetrofitClient.appService().getMyUserInfoData(user.getString("token", "")).enqueue(new Callback<MyUserInfoBean>() {
            @Override
            public void onResponse(Call<MyUserInfoBean> call, Response<MyUserInfoBean> response) {
                MyUserInfoBean myUserInfoBean = response.body();
                if (myUserInfoBean != null && myUserInfoBean.getCode() == 200) {
                    MyUserInfoBean.UserDTO userDTO = myUserInfoBean.getUser();
                    SharedPreferences.Editor edit = user.edit();
                    edit.putString("userId", userDTO.getUserId().toString());
                    edit.apply();
                    Toast.makeText(MyLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<MyUserInfoBean> call, Throwable t) {
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        if (!sharedPreferences.getString("username", "").equals("")) {
            editUsername.setText(sharedPreferences.getString("username",""));
            editPassword.setText(sharedPreferences.getString("password",""));
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }
}